package com.smartcanteen.user.controller.user;

import com.smartcanteen.core.utils.poi.ExcelUtil;
import com.smartcanteen.core.web.controller.BaseController;
import com.smartcanteen.core.web.domain.AjaxResult;
import com.smartcanteen.core.web.page.TableDataInfo;
import com.smartcanteen.log.annotation.Log;
import com.smartcanteen.log.enums.BusinessType;
import com.smartcanteen.security.annotation.RequiresPermissions;
import com.smartcanteen.user.domain.entity.Business;
import com.smartcanteen.user.mapper.DeptMapper;
import com.smartcanteen.user.service.IBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商家信息Controller
 *
 * @author ysj
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/user/business")
public class BusinessController extends BaseController
{
    @Autowired
    private IBusinessService businessService;

    @Autowired
    private DeptMapper deptMapper;
    /**
     * 查询商家信息列表
     */
    @RequiresPermissions("user:business:list")
    @GetMapping("/list")
    public TableDataInfo list(Business business)
    {
        startPage();
        List<Business> list = businessService.selectBusinessList(business);
        return getDataTable(list);
    }

    /**
     * 根据不同校区查询对应的食堂列表信息
     */
    @GetMapping("/canteen/{campusId}")
    public AjaxResult getCanteenList(@PathVariable Long campusId)
    {
        return success(deptMapper.getDept(campusId));
    }




    /**
     * 导出商家信息列表
     */
    @RequiresPermissions("user:business:export")
    @Log(title = "商家信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Business business)
    {
        List<Business> list = businessService.selectBusinessList(business);
        ExcelUtil<Business> util = new ExcelUtil<Business>(Business.class);
        util.exportExcel(response, list, "商家信息数据");
    }

    /**
     * 获取商家信息详细信息
     */
    @RequiresPermissions("user:business:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(businessService.selectBusinessById(id));
    }



    /**
     * 修改商家信息
     */
    @RequiresPermissions("user:business:edit")
    @Log(title = "商家信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Business business)
    {
        return toAjax(businessService.updateBusiness(business));
    }

    /**
     * 删除商家信息
     */
    @RequiresPermissions("user:business:remove")
    @Log(title = "商家信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(businessService.deleteBusinessByIds(ids));
    }
}
